/**
 *  编辑页
 *
 * 作者 zhangwei
 * 日期 2024-09-05 09:48:17
 */
layui.use(['jquery','febs', 'form','laydate','fileUpload', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        laydate = layui.laydate,
        fileUpload = layui.fileUpload,
        form = layui.form,
        $view = $('#baseCautionaryLesson-edit'),
        validate = layui.validate;

    var tenantId = POPUP_DATA.tenantId;
    var fileList = [];

    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if (baseCautionaryLessonData) {
            form.val("baseCautionaryLesson-edit-form", {
                "cautionaryLessonId": baseCautionaryLessonData.cautionaryLessonId,
                "tenantId": baseCautionaryLessonData.tenantId,
                "cautionarySource": baseCautionaryLessonData.cautionarySource,
                "cautionarySourceId": baseCautionaryLessonData.cautionarySourceId,
                "cautionaryTitle": baseCautionaryLessonData.cautionaryTitle,
                "cautionaryDeptId": baseCautionaryLessonData.cautionaryDeptId,
                "cautionaryDept": baseCautionaryLessonData.cautionaryDept,
                "studyDate": baseCautionaryLessonData.studyDate,
                "studyRemark": baseCautionaryLessonData.studyRemark,
                "status": baseCautionaryLessonData.status,
            });
            if(baseCautionaryLessonData.uploadFileInfo != null){
                fileList.push(baseCautionaryLessonData.uploadFileInfo);
            }
        }
    }

    laydate.render({
        elem: "#studyDate",
        type: 'datetime',
    })

    fileUpload.render({
        elem:'#uploadBtn',
        url:ctx + 'base/upload',
        type:'2',
        maxNum:1,
        defaultData: fileList.length>0 ? fileList:[],
        done:function(res){
        }
        ,error: function(){
            layer.msg("服务器异常")
        }
    })

    form.on('submit(baseCautionaryLesson-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        data.tenantId = tenantId;
        data.uploadFileInfo = getFileData();
        console.log(data);
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            if (data.cautionaryLessonId && data.cautionaryLessonId.length > 0) {
                febs.postArray(ctx + 'baseCautionaryLesson/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-baseCautionaryLesson').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'baseCautionaryLesson', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-baseCautionaryLesson').find('#query').trigger('click');
                });
            }
        })
    }

    function getFileData(){
        var fileDatas = $view.find(".fileOne")
        var fileList = [];
        $.each(fileDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
                smallPath: item.dataset.smallpath,
            }
            fileList.push(data);
        })
        return fileList[0];
    }
});